package com.xinghou.platform.server.api.controller;

import com.xinghou.platform.server.api.common.ResponseTools;
import com.xinghou.platform.util.exception.DBExceptionEnums;
import com.xinghou.platform.util.exception.ServiceException;

import org.slf4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinghou.platform.server.api.common.Constants;
import com.xinghou.platform.server.api.log.LoggerFactory;

@Controller
@RequestMapping("/server/api/**")
public class BaseControl {

    private Logger loggerError = LoggerFactory.getServerErrorLogger(BaseControl.class);
    private Logger loggerInfo= LoggerFactory.getServerInfoLogger(BaseControl.class);
    static HttpHeaders respHeader;

    static {
        respHeader = new HttpHeaders();
        respHeader.set("Content-Type", "application/json;charset=UTF-8");
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String handleIOException(Exception ex) {
        ex.printStackTrace();
        loggerError.error("catch excption in BaseControl ", ex);
        String msg=ResponseTools.get().response(new ServiceException(DBExceptionEnums.UA_SYSTEM_US_NULL));
        loggerInfo.info("resInfo={}",msg);
        return msg;
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public String handleMissParaException(Exception ex) {
        ex.printStackTrace();
        loggerError.error("catch excption in BaseControl ", ex);
        String msg=ResponseTools.get().response(new ServiceException(DBExceptionEnums.UA_SYSTEM_US_NULL));
        loggerInfo.info("resInfo={}",msg);
        return msg;
    }
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public String handleServiceException( Exception ex) {
        ex.printStackTrace();
        loggerError.error("catch serviceException in BaseControl ", ex);
        String msg=ResponseTools.get().response(ex);
        loggerInfo.info("resInfo={}",msg);
        return msg;
    }
    public String response(Object o) {
        return ResponseTools.get().response(o);
    }

}
